package demoserver;

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;



public class SMTPClient {



 

 private static String PORT = "465";
  private static String STARTTLS = "true";
    private static String AUTH = "true";
    private static String DEBUG = "true";
    private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private String smtpServer;
    private String fromAddress;
    private String toAddress;
    private String content;
    private String password;
    private String subj;

  public SMTPClient(String toAddress,String subj,String content ) {
   set();
   this.toAddress=toAddress;
   this.subj=subj;
   this.content=content;


  }
  public void set()
      {
       smtpServer="smtp.gmail.com";
       fromAddress="haichaule1990@gmail.com";
      toAddress="haichaule1990@gmail.com";
       content="test";
       subj="day la mail test";
       password="lehaichau";
      }



  




    public void send() {



      try {

        Properties _props = new Properties();

        _props.put("mail.smpt.host", smtpServer);
        _props.put("mail.smtp.user",fromAddress);
        _props.put("mail.smtp.password",password);
        _props.put("mail.smtp.port","587");
        _props.put("mail.smtp.auth",AUTH);
        _props.put("mail.smtp.starttls.enable", STARTTLS);
        _props.put("mail.smtp.debug", DEBUG);

        _props.put("mail.smtp.socketFactory.port", PORT);
        _props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
        _props.put("mail.smtp.socketFactory.fallback", "false");


   

        final Session _mailConnection = Session.getInstance(_props, null);
       final MimeMessage _msg = new MimeMessage(_mailConnection);
       _mailConnection.setDebug(true);



        Address _to = new InternetAddress(toAddress);

        Address _from = new InternetAddress(fromAddress);



        _msg.setContent(content, "text/plain");

        _msg.setFrom(_from);

        _msg.setRecipient(Message.RecipientType.TO, _to);

        _msg.setSubject(subj);



        // This can take a non-trivial amount of time so

        // spawn a thread to handle it.

        Runnable r = new Runnable( ) {

          public void run( ) {

            try {

                  //Use Transport to deliver the message
            Transport transport = _mailConnection.getTransport("smtp");
            transport.connect(smtpServer, fromAddress, password);
            transport.sendMessage(_msg, _msg.getAllRecipients());
            transport.close();
            
            }

            catch (Exception ex) {

            System.out.println("loi xay ra:"+ex.toString());
            
            }

          }

        };

        Thread t = new Thread(r);

        t.start( );



        

      }

      catch (Exception ex) {

        // I should really bring up a more specific error dialog here.

        ex.printStackTrace( );

      }



    }



  }





